
#include "InvoiceItemDialog.h"
#include "Helper.h"
#include "Proveider.h"
#include "ProductsModelDelegate.h"

#include <QMessageBox>
#include <QDialogButtonBox>
#include <QPushButton>
#include <assert.h>

using namespace ecocity;

static const QString all_proveiders = "Todos";

//---------------------------------------------------------------

InvoiceItemDialog::InvoiceItemDialog( Customer::TYPE customer_type,
                              const std::vector< Product >& products,
                              QWidget *parent )
    : QDialog( parent ),
      m_customer_type( customer_type ),
      m_item( customer_type, Product( -1, "", "", Proveider( "" ) ), 0, 0 ),
      m_products_model( products ),
      products_filter_model( new QSortFilterProxyModel( this ) )
{
    setupUi( this );

    setWindowTitle( "Selecciona Producto y cantidad" );

    // Set up Models
    products_filter_model->setSourceModel( &m_products_model );
    products_filter_model->setFilterKeyColumn( ProductsModel::PRODUCT_PROVIDER );

    // Set Views
    products_view->setModel( products_filter_model );
    products_view->setItemDelegate( new ProductsModelDelegate( Helper::get_proveiders( products ), this ) );

    // Set Proveiders ComboBox
    proveider_box->addItem( all_proveiders );
    proveider_box->addItems( Helper::get_proveider_names( products ) );

    hide_prices_for_other_customer_types( customer_type );

    products_view->resizeColumnsToContents( );
    products_view->horizontalHeader()->setStretchLastSection( true );

    // Set Connections
    // Proveider ComboBox changedIndex
    connect( proveider_box, SIGNAL( currentIndexChanged( QString ) ),
             this, SLOT( show_products_from_proveider( QString ) ) );

    connect( products_view, SIGNAL( pressed( QModelIndex ) ),
             this, SLOT( show_or_hide_weight( QModelIndex ) ) );

//    customer_label->setText( customer_details.join( "\n" ) );

//    connect( finnish_button, SIGNAL( rejected() ),
//             this, SLOT( reject() ) );

//    connect( add_invoice_item, SIGNAL( clicked() ),
//             this, SLOT( on_add_invoice_item() ) );
}

//---------------------------------------------------------------

InvoiceItem
InvoiceItemDialog::get_item( ) const
{
    return m_item;
}

//---------------------------------------------------------------

void
InvoiceItemDialog::show_products_from_proveider( QString proveider )
{
    products_filter_model->setFilterKeyColumn( ProductsModel::PRODUCT_PROVIDER );

    if( proveider == all_proveiders )
    {
        proveider = "";
    }

    products_filter_model->setFilterRegExp( proveider );
}

//---------------------------------------------------------------

void
InvoiceItemDialog::accept( )
{
    QItemSelectionModel* model = products_view->selectionModel( );

    if ( !model->hasSelection( ) )
    {
        QMessageBox::warning( this, "Producto no seleccionado", "Selecciona un producto a eliminar" );
        return;
    }

    QModelIndex index = products_view->currentIndex( );

    if ( !index.isValid( ) )
    {
        QMessageBox::warning( this, "Product Index found", "Product Index not found" );
        return;
    }

    int quantity = quantity_spin->value( );
    float weight = weight_spin->value( );
    float discount = discount_spin->value();

    if( quantity == 0 )
    {
        QMessageBox::warning( this, "Cantidad no puede ser cero", "Cantidad no puede ser cero" );
        return;
    }

    Product product = product_from_filter_model( index );

    if( product.price_type() == Product::PER_KILO
            && weight == 0 )
    {
        QMessageBox::warning( this, "Peso no puede ser cero", "Peso no puede ser cero" );
        return;
    }

    m_item.set_product( product );
    m_item.set_quantity( quantity );
    m_item.set_discount( discount );
    m_item.set_weight( weight );

    // Call base class in order to close dialog
    QDialog::accept();
}

//---------------------------------------------------------------

Product
InvoiceItemDialog::product_from_filter_model( const QModelIndex &index )
{
    int product_id =
            products_filter_model->data(
                products_filter_model->index( index.row( ), ProductsModel::PRODUCT_ID ) ).toInt( );

    QString product_name =
            products_filter_model->data(
                products_filter_model->index( index.row( ), ProductsModel::PRODUCT_NAME ) ).toString( );

    QString product_description =
            products_filter_model->data(
                products_filter_model->index( index.row( ), ProductsModel::PRODUCT_DESCRIPTION ) ).toString( );

    QString proveider =
            products_filter_model->data(
                products_filter_model->index( index.row( ), ProductsModel::PRODUCT_PROVIDER ) ).toString( );

    Proveider product_proveider( proveider );

    float product_price =
            products_filter_model->data(
                products_filter_model->index( index.row( ), ProductsModel::PRODUCT_COST_ECOCITY ) ).toFloat();

    int product_tax_rate =
            products_filter_model->data(
                products_filter_model->index( index.row( ), ProductsModel::PRODUCT_TAX_RATE ) ).toInt( );

    QString price_type =
            products_filter_model->data(
                products_filter_model->index ( index.row( ), ProductsModel::PRODUCT_PRICE_TYPE ) ).toString( );

    Product::PRICE_TYPE product_price_type = Product::to_price_type( price_type );

    Product product( product_id,
                     product_name,
                     product_description,
                     product_proveider,
                     product_price,
                     product_price_type, product_tax_rate );

    return product;
}

//---------------------------------------------------------------

void
InvoiceItemDialog::hide_prices_for_other_customer_types( Customer::TYPE customer_type )
{
    switch ( customer_type )
    {
    case Customer::CONSUMIDOR_FINAL:
    {
        products_view->hideColumn( ProductsModel::PRODUCT_PRICE_COOPE );
        products_view->hideColumn( ProductsModel::PRODUCT_PRICE_SHOP );
        break;
    }
    case Customer::COOPERATIVA:
    {
        products_view->hideColumn( ProductsModel::PRODUCT_PRICE_END_CUSTOMER );
        products_view->hideColumn( ProductsModel::PRODUCT_PRICE_SHOP );
        break;
    }
    case Customer::TIENDA:
    {
        products_view->hideColumn( ProductsModel::PRODUCT_PRICE_END_CUSTOMER );
        products_view->hideColumn( ProductsModel::PRODUCT_PRICE_COOPE );
        break;
    }
    default:
        break;
    }
}

//---------------------------------------------------------------

void
InvoiceItemDialog::show_or_hide_weight( const QModelIndex& index )
{
    Product product = product_from_filter_model( index );

    if( product.price_type( ) == Product::PER_KILO )
    {
        weight_spin->setEnabled( true );
    }
    else
    {
        weight_spin->setEnabled( false );
    }
}

